{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# 拆包：元组\n",
    "def return_num():\n",
    "    return 100, 200\n",
    "\n",
    "num1, num2 = return_num()\n",
    "print(num1)  # 100\n",
    "print(num2)  # 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name\n",
      "age\n",
      "TOM\n",
      "18\n"
     ]
    }
   ],
   "source": [
    "# 拆包：字典\n",
    "dict1 = {'name': 'TOM', 'age': 18}\n",
    "a, b = dict1\n",
    "\n",
    "# 对字典进行拆包，取出来的是字典的key\n",
    "print(a)  # name\n",
    "print(b)  # age\n",
    "\n",
    "print(dict1[a])  # TOM\n",
    "print(dict1[b])  # 18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "# 交换变量值1\n",
    "\n",
    "a, b = 10, 20\n",
    "\n",
    "# 1. 定义中间变量\n",
    "c = 0\n",
    "\n",
    "# 2. 将a的数据存储到c\n",
    "c = a\n",
    "\n",
    "# 3. 将b的数据20赋值到a，此时a = 20\n",
    "a = b\n",
    "\n",
    "# 4. 将之前c的数据10赋值到b，此时b = 10\n",
    "b = c\n",
    "\n",
    "print(a)  # 20\n",
    "print(b)  # 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# 交换变量值1\n",
    "\n",
    "a, b = 1, 2\n",
    "a, b = b, a\n",
    "print(a)  # 2\n",
    "print(b)  # 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 引用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1347317216\n",
      "1347317216\n",
      "1\n",
      "1347317248\n",
      "1347317216\n",
      "1521535020104\n",
      "1521535020104\n",
      "[10, 20, 30]\n",
      "1521535020104\n",
      "1521535020104\n"
     ]
    }
   ],
   "source": [
    "# 1. int类型\n",
    "a = 1\n",
    "b = a\n",
    "\n",
    "print(b)  # 1\n",
    "\n",
    "print(id(a))  # 140708464157520\n",
    "print(id(b))  # 140708464157520\n",
    "\n",
    "a = 2\n",
    "print(b)  # 1,说明int类型为不可变类型 \n",
    "\n",
    "print(id(a))  # 140708464157552，此时得到是的数据2的内存地址\n",
    "print(id(b))  # 140708464157520\n",
    "\n",
    "\n",
    "# 2. 列表\n",
    "aa = [10, 20]\n",
    "bb = aa\n",
    "\n",
    "print(id(aa))  # 2325297783432\n",
    "print(id(bb))  # 2325297783432\n",
    "\n",
    "\n",
    "aa.append(30)\n",
    "print(bb)  # [10, 20, 30], 列表为可变类型\n",
    "\n",
    "print(id(aa))  # 2325297783432\n",
    "print(id(bb))  # 2325297783432"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n",
      "1347320384\n",
      "200\n",
      "1347323584\n",
      "[11, 22]\n",
      "1521516710216\n",
      "[11, 22, 11, 22]\n",
      "1521516710216\n"
     ]
    }
   ],
   "source": [
    "def test1(a):\n",
    "    print(a)\n",
    "    print(id(a))\n",
    "\n",
    "    a += a\n",
    "\n",
    "    print(a)\n",
    "    print(id(a))\n",
    "\n",
    "\n",
    "# int：计算前后id值不同\n",
    "b = 100\n",
    "test1(b)\n",
    "\n",
    "# 列表：计算前后id值相同\n",
    "c = [11, 22]\n",
    "test1(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:root] *",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
